package front;

import DAO.TextDAO;
import controller.TextController;
import entity.db.CitationTextDB;
import entity.db.KnowledgeFlowDB;
import entity.text.ChapterEntity;
import entity.text.PaperEntity;
import util.SplitTextInSentence;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：hzs
 * @date ：Created in 2021/1/20 21:05
 * @description：
 * @modified By：
 * @version: $
 */
public class TextFront {

    private TextController textController = new TextController();

    private CitationFront citationFront = new CitationFront();

    public void extractNounPhrase() {
        List<CitationTextDB> allCitationText = textController.getCitationTextNotInKnowledgeFlowTable();
        List<KnowledgeFlowDB> knowledgeFlowDBList = new ArrayList<>();
        for (CitationTextDB citationTextDB : allCitationText) {
            String citation = citationTextDB.getCitation();
            List<String> nounPhraseFromSentence = null;
            try {
                nounPhraseFromSentence = textController.getNounPhraseFromSentence(citation);
            } catch (Exception e) {
                System.out.println("引文抽取失败：" + citationTextDB.getId());
                continue;
            }
            for (String s : nounPhraseFromSentence) {
                if (s.length() > 1) {
                    KnowledgeFlowDB knowledgeFlowDB = new KnowledgeFlowDB();
                    knowledgeFlowDB.setCitationId(citationTextDB.getId());
                    knowledgeFlowDB.setNounPhrase(s);
                    knowledgeFlowDBList.add(knowledgeFlowDB);
                }
            }
            if (knowledgeFlowDBList.size() > 100) {
                textController.batchInsertKnowledgeFlow(knowledgeFlowDBList);
                knowledgeFlowDBList = new ArrayList<>();
            }
        }
        textController.batchInsertKnowledgeFlow(knowledgeFlowDBList);
    }

    /**
     * 名词短语修正（针对公式）
     */
    public void updateNounPhrase() {

    }

    public void calculateDistribution() {
        // id, citation_id, noun_phrase, target, citation_index， citation
        List<List<Object>> knowledgeFlowNotInKFContent = textController.getKnowledgeFlowNotInKFContent();
        // 前置处理：公式修正
        // TODO: 2021/1/23
        // 根据target读文件，有个函数
        int readingFileId = -1;
        PaperEntity paperEntity = null;
        for (List<Object> objects : knowledgeFlowNotInKFContent) {
            try {
                Integer id = (Integer) objects.get(0);
                Integer citationId = (Integer) objects.get(1);
                String nounPhrase = (String) objects.get(2);
                nounPhrase = "complexity computational";
                Integer target = (Integer) objects.get(3);
                String citationIndex = (String) objects.get(4);
                String citation = (String) objects.get(5);

                if (readingFileId != target) {
                    paperEntity = citationFront.readPaperById(target.toString());
                }
                List<ChapterEntity> chapterEntities = paperEntity.getChapterEntities();
                int count = 0;
                for (ChapterEntity chapterEntity : chapterEntities) {
                    String content = chapterEntity.getContent();
                    List<String> list = SplitTextInSentence.testChunkSentences(content);
                    if (list.isEmpty()) {
                        if (citationFront.containNouns(content, nounPhrase)) {
                            count++;
                        }
                    } else {
                        boolean found = false;
                        for (String s : list) {
                            if (citationFront.containNouns(s, nounPhrase)) {
                                found = true;
                                String sql2 = "INSERT INTO kf_content(kf_id, content, label) VALUES(" + id +
                                        ", '" + s +
                                        "', '" + chapterEntity.getIndex() +
                                        "')";
                                new TextDAO().executeWriteSql(sql2);
                            }
                        }
                        if (found) {
                            count++;
                        }
                    }
                }
                double value = 1.0 * count / chapterEntities.size();
                String sql = "UPDATE knowledge_flow SET distribution = " + value +
                        " WHERE id = " + id;
                new TextDAO().executeWriteSql(sql);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 分解文件
        // 搜索：1、计算分布；2、抽取知识流内容
    }

    public static void main(String[] args) {
        new TextFront().calculateDistribution();
    }

}
